﻿<Serializable()> _
Public Class FileInfo
    Implements Runtime.Serialization.ISerializable

    Private FileLocation As String
    Private FileName As String
    Public Checksum As Byte()
    Private Uri As String
    Public InstallBehavior As InstallBehaviors

    Public Enum InstallBehaviors As Byte
        Deleted = 0
        Available = 1
        SearchAndDelete = 255
    End Enum

    Public Property Name As String
        Get
            Return FileName
        End Get
        Set(value As String)
            FileName = value
        End Set
    End Property

    Public Property Location As String
        Get
            Return FileLocation
        End Get
        Set(value As String)
            FileLocation = value
            If My.Computer.FileSystem.FileExists(value) Then
                Dim FileData As Byte() = My.Computer.FileSystem.ReadAllBytes(value)
                Dim SHA512 As New Security.Cryptography.SHA512Managed()
                Checksum = SHA512.ComputeHash(FileData)
                FileName = New IO.FileInfo(value).Name
            End If
        End Set
    End Property

    Public ReadOnly Property SHA512 As String
        Get
            If IsNothing(Checksum) OrElse Checksum.Length = 0 Then Return "Nothing to compute"
            Dim StrOut As String = ""
            For i = 0 To Checksum.Length - 1
                StrOut += Hex(Checksum(i))
            Next
            Return StrOut
        End Get
    End Property

    Public Property URL As String
        Get
            Return Uri
        End Get
        Set(value As String)
            If InstallBehavior = InstallBehaviors.Available Then Uri = value
        End Set
    End Property

    Public Property Availablity As String
        Get
            Select Case InstallBehavior
                Case InstallBehaviors.Available
                    Return "Available"
                Case InstallBehaviors.Deleted
                    Return "Deleted"
                Case InstallBehaviors.SearchAndDelete
                    Return "SearchAndDelete"
                Case Else
                    Return ""
            End Select
        End Get
        Set(value As String)
            Select Case LCase(value)
                Case "available"
                    InstallBehavior = InstallBehaviors.Available
                    If Uri = "" Then Uri = "http://"
                Case "deleted"
                    InstallBehavior = InstallBehaviors.Deleted
                    Uri = ""
                Case "searchanddelete"
                    InstallBehavior = InstallBehaviors.SearchAndDelete
                    Uri = ""
                Case Else
                    Throw New Exception("Not an appropriate value")
            End Select
        End Set
    End Property

    Public Sub New()
        FileName = "filename.extension"
        FileLocation = ""
        Uri = "http://"
        InstallBehavior = InstallBehaviors.Available
    End Sub

    Public Sub New(Location As String)
        Me.Location = Location
        Uri = "http://"
        InstallBehavior = InstallBehaviors.Available
    End Sub

    Public Sub New(info As Runtime.Serialization.SerializationInfo, context As Runtime.Serialization.StreamingContext)
        Me.FileName = info.GetString("FileName")
        Me.FileLocation = ""
        Me.Checksum = CType(info.GetValue("CheckSum", GetType(Byte())), Byte())
        Me.Uri = info.GetString("Uri")
        Me.InstallBehavior = CType(info.GetByte("State"), InstallBehaviors)
    End Sub

    <Security.Permissions.SecurityPermissionAttribute(Security.Permissions.SecurityAction.Demand, SerializationFormatter:=True)> _
    Public Sub GetObjectData(info As Runtime.Serialization.SerializationInfo, context As Runtime.Serialization.StreamingContext) Implements Runtime.Serialization.ISerializable.GetObjectData
        info.AddValue("FileName", FileName)
        info.AddValue("CheckSum", Checksum)
        info.AddValue("Uri", Uri)
        info.AddValue("State", InstallBehavior)
    End Sub

End Class